﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing.Imaging;
using System.Text.RegularExpressions;
using 语文字帖.Models;

namespace 语文字帖.Controllers
{
    public class ChessQueryController : Controller
    {
        private readonly ILogger<HomeController> _logger;

        public ChessQueryController(ILogger<HomeController> logger)
        {
            _logger = logger;
        }

        public IActionResult Index()
        {
            return View();
        }

        public ActionResult Query(string name)
        {
            if (!string.IsNullOrWhiteSpace(name) && name.Length <= 8)
            {
                return Json(ChessQuery.GetMatchResult(name));
            }
            else
            {
                return Json(new List<ChessResult>());
            }
        }

        /// <summary>
        /// 检查是否为全中文
        /// </summary>
        /// <param name="str_chinese"></param>
        /// <returns></returns>
        public bool IsChinese(string str_chinese)
        {
            bool b = false;
            for (int i = 0; i < str_chinese.Length; i++)
            {
                Regex reg = new Regex(@"[\u4e00-\u9fa5]");
                if (!reg.IsMatch(str_chinese[i].ToString()))
                {

                    b = true;
                    break;
                }
            }
            return b;
        }

        public static ImageCodecInfo GetEncoder(ImageFormat format)
        {
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
            foreach (ImageCodecInfo codec in codecs)
            {
                if (codec.FormatID == format.Guid)
                {
                    return codec;
                }
            }
            return null;
        }
        public IActionResult Privacy()
        {
            return View();
        }


        [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
        public IActionResult Error()
        {
            return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
        }
    }


}
